import requests
from loguru import logger

from basic.base_login import merchant_login, user_login
from runner import ENV


def request_hook(response,*args,**kwargs):
    logger.info(f"接口响应信息为:{response.text}")

class BaseRequests:
    def __init__(self):
        self._session = requests.Session()
        self._session.hooks['response'].append(request_hook)
        self._request = self._session





class MerchantRequests(BaseRequests):
    def __init__(self):
        super().__init__()
        self._request.headers.update({"authori-zation":merchant_login(),"content-type":"application/json"})

    def post(self,api_name,url,json):
        response=self._request.post(ENV.MER_HOST+url,json=json)
        logger.info(f"接口{api_name}请求信息为:{url}{json}")
        return response.json

    def get(self, api_name,url,params=None):
        response=self._request.get(ENV.MER_HOST+url,params=params)
        logger.info(f"接口{api_name}请求信息为:{url}{params}")
        return response.json()




class UserRequests(BaseRequests):
    def __init__(self):
        super().__init__()
        self._request.headers.update({"authori-zation":user_login(),"content-type":"application/json"})

    def post(self, api_name, url, json):
        logger.info(f"接口{api_name} 请求信息为: {url} {json}")
        response = self._request.post(ENV.USER_HOST + url, json=json)
        return response.json()

    def get(self, api_name, url, params=None):
        logger.info(f"接口{api_name} 请求信息为: {url} {params}")
        response = self._request.get(ENV.USER_HOST + url, params=params)
        return response.json()


